package com.itheima.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.zip.ZipInputStream;

/**
 * 一
 * 流程定义的部署
 * activiti表有哪些？
 *  act_re_deployment  部署信息
    act_re_procdef     流程定义的一些信息
    act_ge_bytearray   流程定义的bpmn文件及png文件
 */
public class ActivitiDeployment {



    //流程定义部署  流程制作出来后要上传到服务器 zip文件更便于上传
//    public static void main(String[] args) {
//        //1.创建ProcessEngine对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        //2.得到RepositoryService实例
//        RepositoryService repositoryService = processEngine.getRepositoryService();
//
//        //3.转化出ZipInputStream流对象
//        InputStream is = ActivitiDeployment.class.getClassLoader().getResourceAsStream("diagram/holidayBPMN.zip");
//
//        //将 inputstream流转化为ZipInputStream流
//        ZipInputStream zipInputStream = new ZipInputStream(is);
//
//        //3.进行部署
//        Deployment deployment = repositoryService.createDeployment()
//                .addZipInputStream(zipInputStream)
//                .name("请假申请单流程")
//                .deploy();
//
//        //4.输出部署的一些信息
//        System.out.println(deployment.getName());
//        System.out.println(deployment.getId());
//    }


    //流程定义部署
    public static void main(String[] args) throws FileNotFoundException {

////        （2019idea）
        //1.创建ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2.得到RepositoryService实例
        RepositoryService repositoryService = processEngine.getRepositoryService();
//
//        //3.进行部署
//        Deployment deployment = repositoryService.createDeployment()
////                org.activiti.bpmn.exceptions.XMLException: cvc-complex-type.2.4.a: 发现了以元素 'process' 开头的无效内容
//                .disableSchemaValidation() //禁用架构验证
//                .addClasspathResource("diagram/holiday2.bpmn")  //添加bpmn资源
//                .addClasspathResource("diagram/holiday.png")
//                .name("请假申请单流程")
//                .deploy();


//        2022idea  (bpmn20.xml)
            DeploymentBuilder builder = repositoryService.createDeployment();

    // 只能导入文件名称为 [*.bpmn20.xml] 或者 [*.bpmn]  (xml文件路径)
//    File file = new File("/xx/xx/xxx/test.bpmn20.xml");
    Deployment deployment = builder.name("申请流程").addClasspathResource("diagram/holidaynew.bpmn20.xml")
//            .addInputStream(file.getAbsolutePath(), new FileInputStream(file))
            .deploy();

        //4.输出部署的一些信息
        System.out.println(deployment.getName());
        System.out.println(deployment.getId());

    }



}
